package com.swgk.core.view.selection;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.swgk.core.R;
import com.swgk.core.dialog.BasePopWindow;

import java.util.ArrayList;
import java.util.List;

public class MemberStatusView extends RelativeLayout {

    private final FrameLayout rootPopLayout; // eg : fragment_price_adjustment.xml
    private final LinearLayout commonPopLayout;
    private final TextView tvType;
    private final View bottomView;

    private CommonPopWindow mPopup;
    private List<CommonPopEntity> mPopupList;

    private View showBottomView;
    private OnChooseListener onChooseListener;

    private boolean isShowBackground; // 是否显示背景色
    private boolean isCustomPopup; // 是否自定义Popup
    private boolean isShowTriangle; // 是否显示三角
    private boolean isDefaultStyle; // 是否默认样式
    private boolean hasAllTab; // 是否包含默认全部选项
    private String mKeyName = "";
    private String mNo;

    public MemberStatusView(Context context) {
        this(context, null);
    }

    public MemberStatusView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MemberStatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View rootView = LayoutInflater.from(context).inflate(R.layout.common_member_status, this);
        rootPopLayout = rootView.findViewById(R.id.root_layout);
        commonPopLayout = rootView.findViewById(R.id.common_pop_layout);
        tvType = rootView.findViewById(R.id.tv_type);
        bottomView = rootView.findViewById(R.id.bottom_view);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MemberStatusView);
            mKeyName = typedArray.getString(R.styleable.MemberStatusView_key_name);
            isShowBackground = typedArray.getBoolean(R.styleable.MemberStatusView_is_show_background, false);
            isCustomPopup = typedArray.getBoolean(R.styleable.MemberStatusView_custom_popup, false);
            isShowTriangle = typedArray.getBoolean(R.styleable.MemberStatusView_isShowTriangle, true);
            isDefaultStyle = typedArray.getBoolean(R.styleable.MemberStatusView_is_default, true);
            hasAllTab = typedArray.getBoolean(R.styleable.MemberStatusView_hasAllTab, true);
            setBgStyle(commonPopLayout, tvType, 0, mKeyName);
            typedArray.recycle();
        }
        setTag(mKeyName);
        if (!isCustomPopup) {
            commonPopLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    showPop();
                }
            });
        }
        bottomView.setVisibility(GONE);
    }

    public void initPopup(View bgView, View showBottomView, OnChooseListener onChooseListener) {
        this.showBottomView = showBottomView;
        if (showBottomView != null) {
            this.showBottomView.setVisibility(VISIBLE);
        }
        setOnChooseListener(onChooseListener);
        mPopup = new CommonPopWindow(getContext());
        mPopup.setBgView(bgView);
        mPopup.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                CommonPopEntity entity = (CommonPopEntity) key1;
                int position = (int) key3;
                tvType.setText(position == 0 ? mKeyName : entity.getPickerViewText());
                if (onChooseListener != null) {
                    onChooseListener.onChooseResult(mKeyName, entity, position);
                }
            }
        });
        mPopup.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                if (showBottomView != null) {
                    showBottomView.setVisibility(GONE);
                }
                if (mPopup.getCurPos() <= 0) {
                    setBgStyle(commonPopLayout, tvType, 0, null);
                } else {
                    setBgStyle(commonPopLayout, tvType, 1, null);
                }
            }
        });
    }

    public void setKeyName(String mKeyName) {
        this.mKeyName = mKeyName;
        setTag(mKeyName);
        setBgStyle(commonPopLayout, tvType, 0, mKeyName);
    }

    public void refreshChoose(String mNo, String name, int position) {
        this.mNo = mNo;
        if (mPopup != null && !isCustomPopup) {
            mPopup.setDefItem(position);
        }
        if (position == 0) {
            setBgStyle(commonPopLayout, tvType, 0, mKeyName);
        } else {
            setBgStyle(commonPopLayout, tvType, 1, name);
        }
    }

    public void setData(List<CommonPopEntity> popupList) {
        setData(popupList, "");
    }

    public void setData(List<CommonPopEntity> popupList, String mNo) {
        if (mPopup == null) {
            return;
        }
        if (popupList == null || popupList.size() == 0) {
            mPopupList = null;
        } else {
            mPopupList = new ArrayList<>();
            mPopupList.addAll(popupList);
            mPopup.setData(mPopupList);
        }
        this.mNo = mNo;
    }

    public void clearData() {
        this.mNo = "";
        mPopupList = null;
        if (mPopup != null && !isCustomPopup) {
            mPopup.setDefItem(0);
            mPopup.setData(new ArrayList<>());
        }
        updateChooseTextStatus2(commonPopLayout, tvType, 0, mKeyName);
    }

    public void showPop() {
        if (mPopupList == null || mPopupList.size() == 0) {
            if (onChooseListener != null) {
                onChooseListener.onLoadChooseData(mKeyName);
            }
            return;
        }
        if (showBottomView == null) {
            mPopup.showPopwindow0(this);
        } else {
            showBottomView.setVisibility(VISIBLE);
            mPopup.showPopwindow0(showBottomView);
        }
        setBgStyle(commonPopLayout, tvType, TextUtils.isEmpty(mNo) ? -1 : 2, null);
    }

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onLoadChooseData(String mKeyName);

        void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects);
    }

    public String getKeyName() {
        return mKeyName;
    }

    public void updateChooseTextStatus(int value, String name) {
        setBgStyle(commonPopLayout, tvType, value, name);
    }

    private void setBgStyle(LinearLayout layout, TextView textView, int value, String name) {
        if (isDefaultStyle) {
            updateChooseTextStatus(layout, textView, value, name);
        } else {
            updateChooseTextStatus2(layout, textView, value, name);
        }
    }

    /**
     * 更新选项颜色
     */
    private void updateChooseTextStatus(LinearLayout layout, TextView textView, int value, String name) {
        if (!TextUtils.isEmpty(name)) {
            textView.setText(name);
        }
        if (value == -1) {
            setTextViewBg(layout, textView, R.drawable.search_bg, R.mipmap.icon_up_red, R.color.color_tab_one);
        } else if (value == -2) {
            setTextViewBg(layout, textView, R.drawable.shape_blue_stroke_999_bg, R.mipmap.icon_up_red, R.color.color_main);
        } else if (value == 0) {
            if (hasAllTab) {
                setTextViewBg(layout, textView, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_tab_one);
            } else {
                setTextViewBg(layout, textView, R.drawable.shape_blue_stroke_999_bg, R.mipmap.icon_down_red, R.color.color_main);
            }
        } else if (value == 1) {
            setTextViewBg(layout, textView, R.drawable.shape_blue_stroke_999_bg, R.mipmap.icon_down_red, R.color.color_main);
        } else if (value == 2) {
            setTextViewBg(layout, textView, R.drawable.search_bg, R.mipmap.icon_up_red, R.color.color_tab_one);
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void updateChooseTextStatus2(LinearLayout layout, TextView textView, int value, String name) {
        if (!TextUtils.isEmpty(name)) {
            textView.setText(name);
        }
        rootPopLayout.setBackgroundColor(Color.TRANSPARENT);
        if (value == -1) {
            bottomView.setVisibility(VISIBLE);
            rootPopLayout.setBackground(getResources().getDrawable(R.drawable.member_pop_blue_bg2));
            setTextViewBg(layout, textView, R.drawable.trans_bg, R.mipmap.icn_up_arrow_n, R.color.color_tab_one);
        } else if (value == -2) {
            bottomView.setVisibility(VISIBLE);
            rootPopLayout.setBackground(getResources().getDrawable(R.drawable.member_pop_blue_bg2));
            setTextViewBg(layout, textView, R.drawable.trans_bg, R.mipmap.icn_up_arrow_n, R.color.color_tab_one);
        } else if (value == 0) {
            if (hasAllTab) {
                bottomView.setVisibility(GONE);
                setTextViewBg(layout, textView, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_tab_one);
            } else {
                bottomView.setVisibility(GONE);
                setTextViewBg(layout, textView, R.drawable.shape_blue_stroke_999_bg, R.mipmap.icon_down_red, R.color.color_main);
            }
        } else if (value == 1) {
            bottomView.setVisibility(GONE);
            setTextViewBg(layout, textView, R.drawable.shape_blue_stroke_999_bg, R.mipmap.icon_down_red, R.color.color_main);
        } else if (value == 2) {
            bottomView.setVisibility(VISIBLE);
            rootPopLayout.setBackground(getResources().getDrawable(R.drawable.member_pop_blue_bg2));
            setTextViewBg(layout, textView, R.drawable.trans_bg, R.mipmap.icn_up_arrow_n, R.color.color_tab_one);
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void setTextViewBg(LinearLayout layout, TextView textView, int bg, int img, int textColor) {
        if (isShowBackground) {
            layout.setBackground(getResources().getDrawable(bg));
        }
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        if (isShowTriangle) {
            textView.setCompoundDrawables(null, null, drawable_n, null);
        } else {
            textView.setCompoundDrawables(null, null, null, null);
        }
        textView.setTextColor(getResources().getColor(textColor));
    }
    public String getTypeName(){
        return tvType.getText().toString();
    }
}
